package com.example.lanouhn.bantangapp.activities.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.contants.LoginNP;
import com.example.lanouhn.bantangapp.activities.contants.LoginNP_url;
import com.example.lanouhn.bantangapp.activities.utils.HttpUtils;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_LoginNP;
import com.example.lanouhn.bantangapp.activities.utils.SPUtils;



/**
 * Created by lanouhn on 16/8/4.
 */
public class ActivityLogin extends Activity {

    private ImageView back;
    private TextView  resgin;//注册
    private TextView  login;//登录
    private EditText  et_name;//账户输入框
    private EditText  et_pwd;//密码输入框

    private LoginNP lList;//实体类
    private String key;//用户标识
    private String nickname;//用户名标识
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.msg_land_signin_activity);
        initview();
        initLintener();
    }

    //找到控件
    private void initview() {
        back= (ImageView) findViewById(R.id.back);
        resgin= (TextView) findViewById(R.id.resgin);
        login= (TextView) findViewById(R.id.login);
        et_name= (EditText) findViewById(R.id.et_name);
        et_pwd= (EditText) findViewById(R.id.et_pwd);
    }
//点击监听事件
    private void initLintener() {
        //返回
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //注册
        resgin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(ActivityLogin.this,ActivityResgin.class);
                startActivity(it);
            }
        });
       //登录
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_pwd.getText().toString().trim().length()>0&&et_name.getText().toString().trim().length()>0){
                    //判断输入内容是否正确
                    //进行解析数据
                        initDataToPost();
                }else {
                    Toast.makeText(ActivityLogin.this, R.string.Null, Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    //post解析数据
    private void initDataToPost() {


        new Thread(new Runnable() {
            @Override
            public void run() {
               String userName=et_name.getText().toString();
                String userPwd = et_pwd.getText().toString();

                String url = LoginNP_url.loginNP;//获取网络地址
                String params="app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=&oauth_token=&track_deviceid=868596020655823&track_device_info=m2+note&channel_name=UMENG_CHANNEL&app_installtime=1468811189&app_versions=5.8.4&os_versions=5.1&screensize=1080&v=16&username="+userName+"&password="+userPwd+"&grant_type=password&account_auth=d4992bd0839319e73b5fd50f98e7f584";
                String result = HttpUtils.doPost(url,params);

                lList = JsonUtils_LoginNP.getlloginList(result);
                key=lList.getUser_id();
                nickname=lList.getNickname();

                SPUtils.put(ActivityLogin.this,"nickname",nickname);
                SPUtils.put(ActivityLogin.this,"login",key);

                if (null!=result&&result.length()>0){

                    handler.sendEmptyMessage(1);
                }else{
                    handler.sendEmptyMessage(0);
                }

            }


        }).start();
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    Intent it = new Intent(ActivityLogin.this,MainActivity.class);
                        startActivity(it);
                    Toast.makeText(ActivityLogin.this,R.string.sucess,Toast.LENGTH_SHORT).show();
                    break;
                case 0:
                    Toast.makeText(ActivityLogin.this,R.string.fail,Toast.LENGTH_SHORT).show();
                    break;

            }
        }
    };

}
